import { useIntersectionObserver } from '@vueuse/core';
/**
 * 封装一个通用的方法实现数据的懒加载
 * @param {DOM} target表示组件的最外层div元素
 * @returns
 */
export const useLazyData = function (target: Parameters<typeof useIntersectionObserver>[0], callBack: () => void) {
	// 监听组件是否进入可视区
	const { stop } = useIntersectionObserver(target, ([{ isIntersecting }]) => {
		// 如果target对应的DOM进入可视区，那么该回调函数就触发
		if (isIntersecting) {
			// 被监听的DOM进入了可视区:此时调用接口获取数据；停止继续监听
			stop();
			// 回调函数
			if (callBack) callBack();
		}
	});
};

/**
 * 路由跳转打开
 * @param {*} routeInfo
 */
export function navigateNewWindow(routeInfo: string) {
	const resolved = $router.resolve(routeInfo);
	window.open(resolved.href, '_blank');
}

export const autoImport = {
	'@/utils/util': ['selectDictLabel', 'resetForm'],
	'@/router': [['default', '$router']],
	'@/store': ['$store'],
	'@/utils/format': ['numberFormat'],
	'@/utils/msg': ['msgSuccess', 'msgError', 'msgInfo', 'msgWarning', 'confirmFn'],
	'@/hooks/mixin': ['addDateRange', 'dialogMixin', 'pageMixin', 'listMixin', 'treeMixin', 'spreadPage'],
	'@/utils/number': ['add', 'sub', 'mul', 'div', 'calculator'],
	'@/utils/autoImport': ['useLazyData', 'navigateNewWindow'],
	'@/utils/download': ['downloadGet', 'downloadPost'],
	'lodash-es': [
		'camelCase',
		'clone',
		'cloneDeep',
		'merge',
		'isEqual',
		'isArray',
		'uniqBy',
		'isDate',
		'isElement',
		'isFunction',
		'isString',
		'isObject',
		'debounce',
		'isEmpty',
		'unionBy'
	]
};
